angular.module("config")
    .controller("orderController", function ($scope, $http) {

        var score = 10;
        $scope.score = score;
        // 评分样式、回调初始化
        $(".ratyli").ratyli({
            full: "<i class='fa fa-star fa-lg'></i>",
            empty: "<i class='fa fa-star-o fa-lg'></i>",
            onRated: function (value, init) {
                // prevent run at init
                if (!init) {
                    //更改分数数值
                    $("#score").html(value);
                    score = value;
                }
            },
        });


        var req_order = {
            method: "GET",
            url: "order"
        };

        var orders = null;

        function getOrders() {
            $http(req_order)
                .then(function (res) {
                    orders = res.data;
                    var sum = 0;
                    for (var i in orders) {
                        orders[i].sum = 0;
                        switch (orders[i].oStatus) {
                            case 1:
                                orders[i].tClass = "bg-warning";
                                orders[i].status_msg = "待接单";
                                orders[i].date_msg = "下单时间： " + orders[i].startTime;
                                break;
                            case 2:
                                orders[i].tClass = "bg-info";
                                orders[i].status_msg = "已接单";
                                orders[i].date_msg = "接单时间： " + orders[i].confirmTime;
                                break;
                            case 3:
                                orders[i].tClass = "bg-success";
                                orders[i].status_msg = "订单完成";
                                orders[i].date_msg = "完成时间： " + orders[i].finishTime;
                                break;
                            case 4:
                                orders[i].tClass = "bg-danger";
                                orders[i].status_msg = "已拒单";
                                orders[i].date_msg = "完成时间： " + orders[i].finishTime;
                                break;
                        }
                        for (var j in orders[i].foodInfo) {
                            orders[i].foodInfo[j].total = orders[i].foodInfo[j].num * orders[i].foodInfo[j].item.fPrice;
                            orders[i].sum += orders[i].foodInfo[j].total;
                        }
                        // orders[i].sum = sum;
                    }
                    $scope.orders = orders;
                });
        }

        getOrders();

        $scope.setCurrent = function (index) {
            console.log(index);
            $scope.currentOrder = $scope.orders[index];
        };

        $scope.complaint = function () {
            var req = {
                method: "PUT",
                url: "order/" + $scope.currentOrder._id,
                data: {
                    complaint: $scope.currentOrder.complaintContext,
                }
            };
            $http(req).then(function (res) {
                if (!res.data.error) {
                    alert("success");
                    $('#evalution_modal').modal('hide');
                    $scope.currentOrder.oStatus = 4;
                } else {
                    alert("fail-complaint");
                }
            });
        };

        $scope.confirm = function () {
            var req = {
                method: "PUT",
                url: "order/" + $scope.currentOrder._id,
                data: {
                    oStark: score,
                }
            };
            $http(req).then(function (res) {
                if (!res.data.error) {
                    alert("success");
                    $scope.currentOrder.oStatus = 3;
                    $scope.currentOrder.tClass = "bg-success";
                    $scope.currentOrder.status_msg = "订单完成";
                    $scope.currentOrder.date_msg = "完成时间： " + res.data.finishTime;

                    $('#confirm_modal').modal('hide');
                } else {
                    alert("fail-confirm");
                }
            });
        };

        //父子controller通讯
        $scope.$on("UserLogout", function (event, data) {
            $scope.orders = null;
            location.href = "#/";
        });

        $scope.$on("UserLogin", function (event, data) {
            getOrders();
        });


        $scope.$emit("changeTopStyle", true);
    });
